package com.chen.controller;

import com.chen.aop.OperLog;
import com.chen.dto.ResponData;
import com.chen.entity.Fan;
import com.chen.entity.Project;
import com.chen.general.Constans;
import com.chen.general.ModuleType;
import com.chen.general.StateCode;
import com.chen.service.FanService;
import com.chen.service.ProjectService;
import com.chen.until.MyUntils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("api/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;
@Autowired
private FanService fanService;

    @OperLog(operModul = ModuleType.PROJECT, operType = ModuleType.ADD, operDesc = "添加项目")
    @RequestMapping("insertProject")
    public ResponData insertProject(
            @RequestParam(name = "uid") String uid,
            @RequestParam(name = "uname") String uname,
            @RequestParam(name = "pname") String pname) {

        int i = this.projectService.queryProjectNumByPname(pname);
        if(i>0){
            return new ResponData(Constans.REPEAT_ADD, StateCode.ERROR_CODE);
        }

        Project project = new Project()
                .setId(MyUntils.returnId())
                .setCreate_user_name(uname)
                .setState("1")
                .setPname(pname)
                .setCreate_time(MyUntils.returnTime())
                .setCreate_user_id(uid);
        boolean b = this.projectService.insertProject(project);
        if (b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }
    }



    @RequestMapping("initSelectProject")
    public ResponData queryProject(@Param("uid")String uid) {
        List<HashMap> project = this.projectService.queryProject(uid);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE,project);
    }


    @RequestMapping("mangerProject")
    public ResponData mangerProject(@Param("uid")String uid){
        List<HashMap> list = this.projectService.queryProjectAndFansNum(uid);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE,list);
    }

    @RequestMapping("queryProjectRemark")
    public ResponData queryProjectRemark(String pid){
        Project project = this.projectService.queryProjectByPid(pid);
        return  new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE,project);
    }

    @OperLog(operModul = ModuleType.PROJECT, operType = ModuleType.UPDATE, operDesc = "更新项目备注")
    @RequestMapping("updateProjectRemark")
    public  ResponData updateProjectRemark(String pid,String remark){
        Project project = this.projectService.queryProjectByPid(pid);
        project.setRemark(remark);
        boolean b = this.projectService.updateProject(project);
        if (b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }
    }

    @OperLog(operModul = ModuleType.PROJECT, operType = ModuleType.DELETE, operDesc = "删除项目")
    @RequestMapping("deleteProject")
    public ResponData deleteProject(String pid){
        List<Fan> fans = this.fanService.queryFansByProjectId(pid);
        fans.forEach(e->{
            this.fanService.deleteFenById(e);
        });
        boolean b = this.projectService.deleteProject(new Project().setId(pid));
        if (b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }

    }

    @OperLog(operModul = ModuleType.PROJECT, operType = ModuleType.UPDATE, operDesc = "更改项目名称")
    @RequestMapping("updateProjectName")
    public ResponData updateProjectName(String pid,String pname){
        Project project = this.projectService.queryProjectByPid(pid);
        project.setPname(pname);
        boolean b = this.projectService.updateProject(project);
        if (b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }
    }

}
